/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.game.wordpuzzle;

import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author AritomoAbe
 */
public class Game {

    protected int id;
    protected String answer;
    protected String question;
    protected Vector<User> users = new Vector<User>();
    protected User gameOwner = new User();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public void joinGame(String userName) {
        User user = new User();
        user.setName(userName);
        users.add(user);
    }

    public void setOwnerName(String ownerName) {
        gameOwner.setName(ownerName);
    }

    public String getOwnerName() {
        return gameOwner.getName();
    }

    public void splitAnswer() {
        int userCount = users.size();
        int lettersCount = answer.length();
        if (lettersCount >= userCount) {
            int answerI = 0;
            Iterator<User> i = users.iterator();
            while (i.hasNext()) {
                User u = i.next();
                u.setAnswerPart(String.valueOf(answer.charAt(answerI)));
                answerI++;
            }
            if (answerI != lettersCount) {
                User lastUser = users.lastElement();
                String old = lastUser.getAnswerPart();
                String n = answer.substring(answerI - 1, lettersCount);
                lastUser.setAnswerPart(n);
            }
        }
    }

    public User getUser(String userName) {
        User retUser = null;
        Iterator<User> i = users.iterator();
        while (i.hasNext()) {
            User u = i.next();
            if (u.getName().equals(userName)) {
                retUser = u;
                break;
            }
        }
        return retUser;
    }
}
